<template>
  <el-dialog title="点检单选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal=false
             width="80%"
             center
  >
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="记录编码" prop="recordCode">
        <el-input
          v-model="queryParams.recordCode"
          placeholder="请输入记录编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入计划名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划类型" prop="planType">
        <el-select v-model="queryParams.planType" placeholder="请选择计划类型" clearable>
          <el-option
            v-for="dict in dict.type.dv_plan_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备编码" prop="machineryCode">
        <el-input
          v-model="queryParams.machineryCode"
          placeholder="请输入设备编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="machineryName">
        <el-input
          v-model="queryParams.machineryName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="未完成" value="未完成" />
          <el-option label="已完成" value="已完成" />
          <el-option label="已过期" value="已过期" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
    <el-card style="border-radius: 10px">
    <el-table v-loading="loading" :data="recordList" @current-change="handleCurrent" @row-dblclick="handleRowDbClick">
      <el-table-column width="50" align="center" >
        <template v-slot="scope">
          <el-radio v-model="selectedRepairId" :label="scope.row.recordId" @change="handleRowChange(scope.row)">{{""}}</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="记录编码" width="140" align="center" prop="recordCode">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['dv:mainten:record:query']"
          >{{scope.row.recordCode}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="计划类型" align="center" prop="planType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dv_plan_type" :value="scope.row.planType"/>
        </template>
      </el-table-column>
      <el-table-column label="计划名称" align="center" prop="planName" :show-overflow-tooltip="true"/>
      <el-table-column label="设备编码" align="center" prop="machineryCode" :show-overflow-tooltip="true"/>
      <el-table-column label="设备名称" align="center" prop="machineryName" :show-overflow-tooltip="true"/>
      <el-table-column label="品牌" align="center" prop="machineryBrand" :show-overflow-tooltip="true"/>
      <el-table-column label="规格型号" align="center" prop="machinerySpec" :show-overflow-tooltip="true"/>
      <el-table-column label="计划开始时间" align="center" prop="maintenBeginTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maintenBeginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划结束时间" align="center" prop="maintenEndTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maintenEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="finishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == '未完成'" type="info">未完成</el-tag>
          <el-tag v-else-if="scope.row.status == '已完成'" type="success">已完成</el-tag>
          <el-tag v-else type="danger">已过期</el-tag>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getRecord, listRecord, updateRecord } from "@/api/mes/dv/record";
import { listLine } from "@/api/mes/dv/line";

export default {
  name: "MaintenRecord",
  dicts: ['dv_plan_type'],
  data() {
    return {
      selectedRows: undefined,
      showFlag:false,
      selectedRepairId: undefined,
      optType: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备保养记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recordCode: null,
        planName: null,
        planType: null,
        machineryCode: null,
        machineryName: null,
        status: null,
      },
      // 点检保养记录实体
      record: {},
      // 表单校验
      rules: {},
      // 设备保养记录行表格数据
      lineList: [],
      // line查询参数
      lineParams: {
        pageNum: 1,
        pageSize: 10,
        recordId: null,
      },
    };
  },
  created() {
    console.log("ddddjjjjjjjjj")
    this.getList();

  },
  methods: {
    /** 查询设备保养记录列表 */
    getList() {
      console.log("dddddddddjjjjjjjjjjjjjjjjjjj1111111111111")
      this.selectedRows=[]
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    handleRowDbClick(row){
      if(row){
        this.selectedRows = row;
        this.$emit('onSelected',this.selectedRows);
        this.showFlag = false;
        this.selectedRows=[]
      }
    },
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    //确定选中
    confirmSelect() {
      if (this.selectedRepairId == null || this.selectedRepairId == 0) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected', this.selectedRows);
      this.showFlag = false;
      this.selectedRows=[]
      this.selectedRepairId=null
    },
    // 取消按钮
    cancel() {
      this.selectedRows=[]
      this.showFlag = false;
    },
    /** 查看按钮操作 **/
    handleView(row) {
      const recordId = row.recordId || this.ids;
      getRecord(recordId).then(response => {
        this.record = response.data;
        this.open = true;
        this.title = "查看点检保养记录";
        this.optType = "view";
      });
      this.getLineList(recordId);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const recordId = row.recordId || this.ids
      getRecord(recordId).then(response => {
        this.record = response.data;
        this.open = true;
        this.title = "修改设备保养记录";
        this.optType = "edit";
      });
      this.getLineList(recordId);
    },
    /** 提交按钮 */
    submitForm() {
      if (this.record.recordId != null) {
        this.record.lineList = this.lineList;
        updateRecord(this.record).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      }
    },
    // 完成提交
    Finish() {
      let list = this.lineList;
      for (let i = 0; i < list.length; i++) {
        if (list[i].result == null) {
          this.$modal.msgError("请填写项目结果！");
          return;
        }
      }
      if (this.record.recordId != null) {
        if (this.isTimeBeforeNow(this.record.maintenEndTime)) {
          this.record.status = '已过期';
        }else {
          this.record.status = '已完成';
        }
        this.record.lineList = this.lineList;
        updateRecord(this.record).then(response => {
          this.$modal.msgSuccess("提交成功");
          this.open = false;
          this.getList();
        });
      }
    },
    /** 查询设备保养记录行列表 */
    getLineList(recordId) {
      this.lineParams.recordId = recordId
      listLine(this.lineParams).then(response => {
        this.lineList = response.rows;
      });
    },
    isTimeBeforeNow(str) {
      const date = new Date(str);
      if (isNaN(date.getTime())) {
        this.$modal.msgError("时间格式错误");
        return false
      }
      const now = new Date();
      now.setHours(0, 0, 0, 0);
      const targetDate = new Date(date.getFullYear(), date.getMonth(), date.getDate())
      return targetDate < now;
    },
  }
};
</script>
